package com.txr.news.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.txr.news.customexception.BusinessException;
import com.txr.news.pojo.Code;
import com.txr.news.pojo.ExType;
import com.txr.news.pojo.Result;
import com.txr.news.pojo.entity.ArticleCate;
import com.txr.news.service.ArticleCateService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/my/article")
public class ArticleCateController {
    ArticleCateService articleCateService;

    @GetMapping("/cates")
    public Result getCates(){
        Result result = new Result(0,"获取文章分类列表成功！", Code.QueryAllSucc);
        List<ArticleCate> cateList = articleCateService.getCates();
        if (cateList == null || cateList.isEmpty()){
            result.setStatus(1);
            result.setMsg("获取文章分类列表失败！");
        }
        result.setData(cateList);
        return result;
    }

    @PostMapping("/addcates")
    public Result addCates(ArticleCate articleCate){
        Result result = new Result(0,"新增文章分类成功！");
        Boolean flag = articleCateService.addCate(articleCate);
        if(!flag){
            result.setStatus(1);
            result.setMsg("类别已存在，新增文章分类失败！");
        }
        return result;
    }

    @GetMapping("/deletecate/{id}")
    public Result deleteCate(@PathVariable String id){
        Result result = new Result(0,"删除文章分类成功！");
        Boolean flag = articleCateService.deleteCate(id);
        if (!flag){
            result.setStatus(1);
            result.setMsg("删除文章分类失败！");
        }
        return result;
    }

    @GetMapping("/cates/{id}")
    public Result getCateById(@PathVariable String id){
        Result result = new Result(0,"获取文章分类数据成功！",null, Code.QueryOneSucc);

        ArticleCate articleCate = articleCateService.getCateById(id);
        result.setData(articleCate);
        return result;
    }

    @PostMapping("/updatecate")
    public Result updateCate(ArticleCate articleCate){
        Result result = new Result(0,"更新分类信息成功！");
        Boolean flag = articleCateService.updateCate(articleCate);
        if (!flag){
            result.setStatus(1);
            result.setMsg("类别名已存在，更新分类信息失败！");
        }
        return result;
    }
}
